<template>
  <div class="xtx-city">
    <div class="select" @click="toggleAtive">
      <span class="placeholder"  v-if="!provinceName">请选择配送地址</span>
      <span class="value" >
          {{provinceName}} {{cityName}} {{countyName}}
      </span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="option" v-if="active">
      <span class="ellipsis" v-for="i in filterDateList" :key="i" @click="changeCity(i)">{{i.name}}</span>
    </div>
  </div>
</template>

<script>
import { reactive, onMounted, ref, computed, toRefs, watch } from 'vue'
import axios from 'axios'
export default {
  name: 'XtxCity',
  emits: ['change'],
  setup (props, { emit }) {
    const dataList = ref([])
    function getCityList () {
      axios({
        url: 'https://yjy-oss-files.oss-cn-zhangjiakou.aliyuncs.com/tuxian/area.json',
        method: 'GET'
      }).then((res) => {
        console.log(res)
        dataList.value = res.data
      })
    }
    onMounted(() => {
      getCityList()
    })
    // 控制选择城市的显示和隐藏
    const active = ref(false)
    function toggleAtive () {
      active.value = !active.value
    }
    const changeResult = reactive({
      provinceCode: '', // 省code
      provinceName: '', // 省名称
      cityCode: '', // 城市code
      cityName: '', // 城市名称
      countyCode: '', // 地区code
      countyName: '' // 地区名称
    })
    //    监听弹层的关闭,如果关闭了,清空数据
    watch(() => { return active.value }, (newVal) => {
      if (newVal === true) {
        changeResult.provinceCode = ''
        changeResult.provinceName = ''
        changeResult.cityCode = ''
        changeResult.cityName = ''
        changeResult.countyCode = ''
        changeResult.countyName = ''
      }
    })
    // 点击的时候,判断点击的level的值
    function changeCity (curItem) {
      // 记录省份的数据
      if (curItem.level === 0) {
        changeResult.provinceCode = curItem.code
        changeResult.provinceName = curItem.name
      }
      // 记录市的数据
      if (curItem.level === 1) {
        changeResult.cityCode = curItem.code
        changeResult.cityName = curItem.name
      }
      // 记录县区的数据
      if (curItem.level === 2) {
        changeResult.countyCode = curItem.code
        changeResult.countyName = curItem.name
      }
      emit('change', changeResult)
    }
    const filterDateList = computed(() => {
      let newDataList = dataList.value
      // 如果省的code有的话,直接在使用,在省列表中查找符合条件的,并且把areaList赋值给newDataList
      if (changeResult.provinceCode) {
        const item = dataList.value.find(item => item.code === changeResult.provinceCode)
        newDataList = item.areaList
      }
      // 如果市的code有的话,直接在使用,在市列表中查找符合条件的,并且把areaList赋值给newDataList
      if (changeResult.cityCode) {
        const item = newDataList.find(item => item.code === changeResult.cityCode)
        newDataList = item.areaList
      }
      // 如果县区的code有的话,直接关闭弹窗
      if (changeResult.countyCode) {
        // eslint-disable-next-line vue/no-side-effects-in-computed-properties
        active.value = false
      }
      return newDataList
    })
    return {
      dataList,
      toggleAtive,
      active,
      changeCity,
      ...toRefs(changeResult),
      filterDateList
    }
  }
}
</script>
<style scoped lang="less">
.xtx-city {
  display: inline-block;
  position: relative;
  z-index: 400;
  margin-left: 10px;
  .select {
    border: 1px solid #e4e4e4;
    height: 30px;
    padding: 0 5px;
    line-height: 28px;
    cursor: pointer;
    &.active {
      background: #fff;
    }
    .placeholder {
      color: #999;
    }
    .value {
      color: #666;
      font-size: 12px;
    }
    i {
      font-size: 12px;
      margin-left: 5px;
    }
  }
  .option {
    width: 542px;
    border: 1px solid #e4e4e4;
    position: absolute;
    left: 0;
    top: 29px;
    background: #fff;
    min-height: 30px;
    line-height: 30px;
    display: flex;
    flex-wrap: wrap;
    padding: 10px;
    > span {
      width: 130px;
      text-align: center;
      cursor: pointer;
      border-radius: 4px;
      padding: 0 3px;
      &:hover {
        background: #f5f5f5;
      }
    }
  }
}
</style>
